//
//  CZMainTableViewController.swift
//  XLWB009
//
//  Created by apple on 3/22/16.
//  Copyright © 2016 apple. All rights reserved.
//

import UIKit

class CZMainTableViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabBar是只读的，不能直接使用==直接赋值
        //KVC进行替换
        let mainTabBar=CZMainTabbar()
        
        //创建自定义tabbar,添加加号按钮
        self.setValue(mainTabBar, forKey: "tabBar")
        
        //准备闭包
        mainTabBar.composeClosure={() -> ()in
            
            print("按钮被点击")
        }
        //添加所有按钮
        addChildViewControllers()
        
    }
    
    func addChildViewControllers(){
        //设置首页
        let home=CZHomeViewController()
        addChildViewController(home, title: "首页", imageName: "tabbar_home")
        
        //设置消息
        let mess=CZMessViewController()
        addChildViewController(mess, title: "消息", imageName: "tabbar_message_center")
        
        //设置发现
        let discover=CZDisViewController()
        addChildViewController(discover, title: "发现", imageName: "tabbar_discover")
        
        //设置我
        let profile=CZProfileViewController()
        addChildViewController(profile, title: "我", imageName: "tabbar_profile")
        
        
    }
    
    func addChildViewController(controller:UIViewController,title: String,imageName:String) {
        
        //设置标题
        controller.title=title
        //设置图片
        controller.tabBarItem.image=UIImage(named:imageName)
        //拼接图片
        let image=imageName+"_highlighted"
        //设置高亮图片
        controller.tabBarItem.selectedImage=UIImage(named:image)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置字体
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //添加控制器
        addChildViewController(UINavigationController(rootViewController: controller))
        
        
    }

}
